/**
 * Created by John on 2016/6/15.
 */
function displayWords(msg, cursor, pretty) {
    print('\n' + msg);
    var words = cursor.map(function (word) {
        return word.word;
    });
    var wordStr = JSON.stringify(words);
    if (wordStr.length > 65) {
        wordStr = wordStr.slice(0, 50) + '...';
    }
    print(wordStr);
}

var mongo = new Mongo('loclhost');
var db = nongo.getDB('BeginningMongoDB');
var words = db.getCollection('word_stats');
var cursor = words.find({first: {$in: ['a', 'b', 'c']}});
displayWords('Words staring wiht a, b or c: ', cursor);

cursor = words.find({size: {$gte: 12}});
displayWords('Words longer than 12 characters: ', cursor);

cursor = words.find({size: {$mod: [2, 0]}});
displayWords('Words with even lengths: ', cursor);

cursor = words.find({letters: {$size: 9}});
displayWords('Words with 9 Distinct characters: ', cursor);

var vowels = ['a', 'e', 'i', 'o', 'u'];

cursor = words.find({
    $and: [
        {
            first: {
                $in: vowels
            }
        },
        {
            last: {
                $in: vowels
            }
        }
    ]
});
displayWords('Words that start and end with a vowel: ', cursor);

cursor = words.find({'stats.vowels': {$gt: 3}});
displayWords('Words containing 4 or more vowels: ', cursor);

cursor = words.find({letters: {$all: vowels}});
displayWords('Words with all 5 vowels: ', cursor);

cursor = words.find({otherChars: {$exists: true}});
displayWords('Words with non-alphabet characters: ', cursor);

cursor = words.find({
    charsets: {
        $elemMatch: {
            $and: [
                {type: 'other'},
                {chars: {$size: 1}}
            ]
        }
    }
});
displayWords('Words with 1 non-alpabet characters: ', cursor);